<template>
  <div class="front">
    <!-- <img :src="`${imgUrl}/${currImg}`" alt="" class="bgimg"> -->
    <el-row class="header">
      <el-col :span="16">
        <div class="nav">
          <ul class="nav-ul">
            <li :class="{active: $route.path === '/home'}"><router-link to="/home">首页</router-link></li>
            <li :class="{active: $route.path === '/category'}" class="category-nav">
              <router-link to="">分类</router-link>
              <div class="category">
                <div class="category-list">
                  <span v-for="item in categories" 
                    :key="item.id"
                    @click="toCategory(item)"
                  >
                  {{item.name}}
                  </span>                 
                </div>
              </div>
            </li>
            <li :class="{active: $route.path === '/tag'}"><router-link to="/tag">标签</router-link></li>
          </ul>
          <div class="search">
            <input v-model="searchText" @keyup.enter="searchHandler" type="text" name="searchText" placeholder="请输入检索内容">
            <el-button size="small" style="margin-left: 5px" @click="searchHandler">搜索</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row class="main">
      <el-col :span="12" :offset="4">
        
        <div class="main-content">
          <router-view />
        </div>
        
      </el-col>
      
      <el-col :span="4" :offset="0" class="hot">
        <!-- 侧边栏 -->
        <h3 style="padding: 10px 0 5px 0; margin-left: 20px">热门文章</h3>
        <div class="hot-article">
          <h3 v-if="hotList.length === 0" >数据加载失败</h3>
          <div class="hot-item" 
          v-for="item in hotList" 
          :key="item.id" >
            <div class="title" @click="to(item.id)" :title="item.title">
              {{item.title}}
            </div>

          </div>
        </div>
        
      </el-col>
    </el-row>
    <!-- <router-view /> -->
    <div class="toTop" v-show="isTop">
      <!-- 111 -->
      
        <a href="#">
          <i class="el-icon el-icon-top"/>
        </a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      // imgUrl: `${process.env.VUE_APP_URL}/download`,
      imgUrl: `${process.env.VUE_APP_URL}/download`,
      currImg: 'ff47fa09-b95d-4d5d-8aa8-79fcf61e580c.jpeg',
      hotList: [],
      categories: [],
      isTop: false,
    }
  },
  mounted() {
    this.getHotList()
    this.GetCategories()
    const body = document.querySelector('body')
    // console.log(body);
    this.$scrollListener(window, this.handleTopTip)
  },
  methods: {
    handleTopTip(e) {
      if (e.target !== document)
        return
      const { scrollTop, clientHeight} = e.target.scrollingElement || e.srcElement.scrollingElement
      if (scrollTop > clientHeight) {
        this.isTop = true
      } else {
        this.isTop = false
      }
    },
    GetCategories() {
      console.log("loading")
      this.$http.get("/front/category/list").then((res) => {
        if (!res.data.status) {
          return this.$message.error("获取分类失败")
        }

        this.categories = res.data.data
      })
    },
    to(id) {
      console.log(id);
      this.$router.push(`/articleDetail/${id}`).catch(e => e);
    },
    toCategory(item) {
      console.log(item);
      this.$router.push({
        path: `/category/${item.id}`,
        query: {
          categoryName: item.name
        }
      }).catch(e => e);
    },
    searchHandler() {
      if (this.searchText === '') {
        return
      }
      this.$router.push({
        path: '/searchList',
        query: {
          wd: this.searchText
        }
      }).catch(e=>e)
      this.searchText = ''
    },
    handleDetail(id) {
      this.$router.push('/articleDetail/' + id).catch(e => e)
    },
    getHotList() {
      this.$http.get("/front/article/hotArticleList").then((res) => {
        if (!res.data.status) {
          return this.$message.error("获取文章失败！")
        }
        const list = res.data.data
        this.hotList = list
      })
    },
  }
}
</script>

<style lang="less" scoped>
.front {
  background-color: #f4f4f4;
  min-height: 100vh;
}
  .bgimg {
    width: 100vw;
    height: 100vh;
    position: absolute;
    position:fixed;
    left: 0;
    top: 0;
    
  }
  .header{
      position: fixed;
      // top: 0;
      // left: 0;
      width: 100%;;
      // width: 100%;
      /* border: 1px solid #ccc; */
      background-color: #fff;
      display: flex;
      justify-content: space-between;
      /* box-shadow: x轴距离 y轴距离 模糊半径 */
      box-shadow:  0 1px 2px 0 rgb(0 0 0 / 10%);
      z-index: 999;
      .nav {
        // position: fixed;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        input {
          display:inline-block;
          padding: 8px 10px;
          outline: none;
          border: none;
          background: rgba(242,242,242,0.75);
          transition: border-color .5s cubic-bezier(.645,.045,.355,1);
          transition: background-color .5s cubic-bezier(.645,.045,.355,1);
          &:focus {
            border: 1px solid #2196f3;
          }
          &::placeholder {
            font-size: 0.8rem;
            padding-left: 10px;
          }
        }
      }
      .nav-ul {
        display: flex;
        &>li>a{
          display: block;
          padding: 1rem 15px;
          color: #333;
          font-size: 1.3em;
          transition: all 0.2s;
          &:hover {
            color: #006d87;
            // box-shadow: 0 3px 0 #006d87;
          }
        }
        .category-nav {
          position: relative;
        }
        .category-nav:hover > .category {
          display: block;
        }
        .category {
          max-height: 300px;
          background: #000;
          position: absolute;
          display: none;
          background-color: #fff;
          padding: 5px;
          // border: 1px solid #006d87;
          // border-bottom: 1px solid #ccc;
          // border-left: 1px solid #ccc;
          // border-right: 1px solid #ccc;
          box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);

        }
        .category-list {
          display: flex;
          flex-direction: row;
          flex-flow: wrap;
          writing-mode: vertical-lr;
          max-height: 20rem;
          // box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
          

          span {
            writing-mode: horizontal-tb;
            cursor: pointer;
            min-width: 5rem;
            max-width: 10rem;
            padding: 5px;
            border-radius: 5px;
            &:hover {
              background-color: #006d87;
              color: #fff;
            }
            
          }
        }
      }
      
  }

  .main-content {
    border-right: 1px solid rgba(127, 127, 127, 0.1);
    padding-right: 0.5rem;
    padding-top: 60px;
  }

  .active {
    // color: #006d87 !important;
    // box-shadow: 0 3px 0 #006d87;
  }

  .category li:hover .category-nav {
    background: #000;
  }

.hot {
  padding-top: 60px;
  position: fixed;
    right: 16.7%;
}
.hot-article {
    // position: fixed;
    // // width: 100%;
    // left: ;
    // right: 16.6%;
  // width: calc(100% - 10px);
  
  min-height: 200px;
  background-color: #fff;
  border-radius: 5px;
  // border: 1px solid #ccc;
  // padding: 10px;
  margin: 10px 0 10px 0.75rem;
  &:hover {
    box-shadow:  0 2px 6px 0 rgb(0 0 0 / 10%);
  }

  h3 {
    text-align: center;
  }

  .hot-item {
    color: #333;
    display: flex;
    justify-content: space-between;
    
    // background-color: #ccc;
    // border-bottom: 1px solid #ccc;
    &:nth-last-child(1) {
      border: none;
    }
    
    .title {
      // padding: 10px 0;
      margin: 5px 15px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      font-weight: bold;
      cursor: pointer;
      &:hover {
        text-decoration: underline;
      }
    }
  }

}

.toTop > * {
  width: 3rem;
  height: 3rem;
  text-align: center;
  line-height: 3rem;
  cursor: pointer;
  position: fixed;
  border-radius: 50%;
  right: 3rem;
  bottom: 3rem;
  font-size: 2rem;
  background-color: #fff;
  transition: linear 0.2s;
  &:hover {
    color: #fff;
    background: #006d87;
  }
}



</style>